package com.masterclient.app.modules.goods;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.eoner.baselibrary.bean.goods.CouponMessage;
import com.eoner.baselibrary.utils.Utils;

public class CommodityCouponAdapter extends BaseQuickAdapter<CouponMessage, BaseViewHolder> {

    private Context mContext;

    public CommodityCouponAdapter(Context mContext) {
        super(R.layout.item_coupon_goods2);
        this.mContext = mContext;
    }

    @Override
    protected void convert(BaseViewHolder helper, CouponMessage item) {
        helper.setText(R.id.tv_money_sign, "¥");
        Utils.setDin(helper.getView(R.id.tv_money_sign), mContext);
        helper.setText(R.id.tv_money, item.getAmount());
        Utils.setDin(helper.getView(R.id.tv_money), mContext);
        helper.setText(R.id.tv_limit, item.getName());

        if (helper.getLayoutPosition() == 1){
            helper.setGone(R.id.view_top_line, true);
        }else {
            helper.setGone(R.id.view_top_line, false);
        }

        View ll_by_money = helper.getView(R.id.ll_by_money);
        View right_layout = helper.getView(R.id.right_layout);
        TextView tvMoneySign = helper.getView(R.id.tv_money_sign);
        TextView tvMoneyDiscountSign = helper.getView(R.id.tv_money_discount_sign);
        TextView tvCouponDate = helper.getView(R.id.tv_coupon_date);
        TextView tv_btn = helper.getView(R.id.tv_btn);
        View received_icon = helper.getView(R.id.received_icon);
        View viewCouponDown = helper.getView(R.id.view_coupon_down);
        ImageView ivCouponDown = helper.getView(R.id.iv_coupon_down);
        TextView tvCouponMs = helper.getView(R.id.tv_coupon_ms);
        View viewDes = helper.getView(R.id.view_des);
        TextView tvMoneyDes = helper.getView(R.id.tv_money_des);
        CheckBox checkBox = helper.getView(R.id.checkBox);
        checkBox.setVisibility(View.GONE);

        if (!TextUtils.isEmpty(item.getDetailDesc())){
            tvMoneyDes.setVisibility(View.VISIBLE);
            tvMoneyDes.setText(item.getDetailDesc());
        }else {
            tvMoneyDes.setVisibility(View.GONE);
        }

        if ("by_fixed".equals(item.getSimple_action())) {
            tvMoneySign.setVisibility(View.VISIBLE);
            tvMoneyDiscountSign.setVisibility(View.GONE);
        } else {
            tvMoneySign.setVisibility(View.GONE);
            tvMoneyDiscountSign.setVisibility(View.VISIBLE);
        }

        if ("0".equals(item.getFont_type())){
            //文本
            tvCouponDate.setTextSize(12);
        }else {
            //时间段
            tvCouponDate.setTextSize(9);
        }
        tvCouponDate.setText(item.getFont_coupon_desc());
        tvCouponMs.setText(item.getConponDesc());

        ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
        viewCouponDown.setOnClickListener(view -> {
            if (item.isExpanded()) {
                item.setIsExpanded(false);
                ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
                tvCouponMs.setVisibility(View.GONE);
                viewDes.setVisibility(View.GONE);
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
            } else {
                item.setIsExpanded(true);
                ivCouponDown.setImageResource(R.mipmap.icon_coupon_up);
                tvCouponMs.setVisibility(View.VISIBLE);
                viewDes.setVisibility(View.VISIBLE);
                ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left_expanded);
                right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right_expanded);
            }
        });
        if (item.isIs_receive()) {
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
            tv_btn.setText("立即使用");
            tv_btn.setBackgroundResource(R.drawable.bg_rect_buy_black);
            tv_btn.setTextColor(Color.parseColor("#FF4646"));
            received_icon.setVisibility(View.VISIBLE);
        } else {
            helper.addOnClickListener(R.id.tv_btn);
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
            tv_btn.setText("立即领取");
            tv_btn.setBackgroundResource(R.drawable.bg_rect_buy_black);
            tv_btn.setTextColor(Color.parseColor("#FFFFFF"));
            received_icon.setVisibility(View.GONE);
        }
        FrameLayout.LayoutParams linearParams =(FrameLayout.LayoutParams) tv_btn.getLayoutParams();
        linearParams.width = ScreenUtils.dp2px(48);
        tv_btn.setLayoutParams(linearParams);
        tv_btn.setText("领券");
        tv_btn.setBackgroundResource(R.drawable.bg_rect_buy_black);
        tv_btn.setTextColor(Color.parseColor("#ffffff"));

        if (item.isExpanded()) {
            ivCouponDown.setImageResource(R.mipmap.icon_coupon_up);
            tvCouponMs.setVisibility(View.VISIBLE);
            viewDes.setVisibility(View.VISIBLE);
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left_expanded);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right_expanded);
        } else {
            ivCouponDown.setImageResource(R.mipmap.icon_coupon_down);
            tvCouponMs.setVisibility(View.GONE);
            viewDes.setVisibility(View.GONE);
            ll_by_money.setBackgroundResource(R.mipmap.bg_uesless_coupon_left);
            right_layout.setBackgroundResource(R.drawable.bg_useless_coupon_right);
        }
    }
}
